#include "mysettings.h"

#include <QDir>

QString MySettings::appName = "meegim";
QString MySettings::pathMeegIMHome = QDir::homePath() + QDir::separator() + ".config" + QDir::separator() + appName;
QString MySettings::pathMeegIMCache = pathMeegIMHome + QDir::separator() + QString("cache");
QString MySettings::fileConfig = pathMeegIMHome + QDir::separator() + MySettings::appName + ".conf";

MySettings::MySettings(QObject *parent) : QSettings(MySettings::fileConfig, QSettings::NativeFormat , parent)
{
    group_main = "main";
    group_gui = "gui";
    group_xmpp = "xmpp";
    group_accounts = "accounts";

    key_jid = "jid";
    key_passwd = "passwd";
    key_showGroup = "show_groups";
    key_lastPresence = "show_last_presence";
    key_accounts = "accounts";
    key_resource = "resource";
    key_status_text = "status_text";
    key_status = "status";
    key_default = "is_default";
    key_useHostPort = "use_host_port";
    key_host = "host";
    key_port = "port";

    key_sndIncomingMsg = "snd_incoming_message";
    key_sndOutcomingMsg = "snd_outcoming_message";

    key_notifyConnection = "notify_connection";
    key_notifyOnline = "notify_online";
    key_notifyOffline = "notify_offline";
    key_notifyMessage = "notify_message";
    key_notifyTyping = "notify_typing";

    key_sndBuddyLogsIn = "snd_buddy_logs_in";
    key_sndBuddyLogsOut = "snd_buddy_logs_out";

    key_reconnectOnError = "reconnect_when_error";
    key_keepAliveInterval = "keep_alive_interval";

    key_sndChatSome1SaysMyName = "sndchat_someone_says_my_name";
    key_sndChatPersonEnters = "sndchat_person_enters";
    key_sndChatPersonLeaves = "sndchat_person_leaves";
    key_sndChatOthersTalk = "sndchat_others_talk";
    key_sndChatITalk = "sndchat_I_talk";

    key_vibrMessageRecv = "vibration_msg_recv";
    key_vibrMessageSent = "vibration_msg_sent";
    key_vibrChatSaysMyName = "vibration_chat_says_my_name";
    key_vibrChatOthersTalk = "vibration_chat_others_talk";

    key_smilesTheme = "smiles_theme";
}

QStringList MySettings::getListAccounts()
{
    beginGroup( group_accounts );
    QVariant ret = value( key_accounts, QStringList() );
    endGroup();
    return ret.toStringList();
}

void MySettings::addAccount( const QString &acc )
{
    beginGroup( group_accounts );
    QVariant retList = value( key_accounts, QStringList() );
    QStringList sl = retList.toStringList();
    if( sl.indexOf(acc) < 0 ) {
        sl.append(acc);
        setValue( key_accounts, QVariant(sl) );
    }
    endGroup();
}

void MySettings::remAccount( const QString &acc )
{
    beginGroup( group_accounts );
    QVariant retList = value( key_accounts, QStringList() );
    QStringList sl = retList.toStringList();
    if( sl.indexOf(acc) >= 0 ) {
        sl.removeOne(acc);
        setValue( key_accounts, QVariant(sl) );
    }
    endGroup();
}

/*-------------------*/

QString MySettings::getPasswd(const QString &jid)
{
    beginGroup( jid );
    QVariant ret = value( key_passwd, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setPasswd(const QString &jid, const QString &passwd)
{
    beginGroup( jid );
    setValue( key_passwd, QVariant(passwd) );
    endGroup();
}


/*-----------*/


QString MySettings::getResource(const QString &jid)
{
    beginGroup( jid );
    QVariant ret = value( key_resource, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setResource(const QString &jid, const QString &resource)
{
    beginGroup( jid );
    setValue( key_resource, QVariant(resource) );
    endGroup();
}


/*-----------*/


bool MySettings::isAccDefault(const QString &jid)
{
    beginGroup( jid );
    QVariant ret = value( key_default, false );
    endGroup();
    return ret.toBool();
}

void MySettings::setAccDefault(const QString &jid, const bool &def)
{
    beginGroup( jid );
    setValue( key_default, QVariant(def) );
    endGroup();
}


/*-----------*/


bool MySettings::isHostPortManually(const QString &jid)
{
    beginGroup( jid );
    QVariant ret = value( key_useHostPort, false );
    endGroup();
    return ret.toBool();
}

void MySettings::setHostPortManually(const QString &jid, const bool &def)
{
    beginGroup( jid );
    setValue( key_useHostPort, QVariant(def) );
    endGroup();
}

/*-----------*/


QString MySettings::getHost(const QString &jid)
{
    beginGroup( jid );
    QVariant ret = value( key_host, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setHost(const QString &jid, const QString &host)
{
    beginGroup( jid );
    setValue( key_host, QVariant(host) );
    endGroup();
}


/*-----------*/


int MySettings::getPort(const QString &jid)
{
    beginGroup( jid );
    QVariant ret = value( key_port, 0 );
    endGroup();
    return ret.toInt();
}

void MySettings::setPort(const QString &jid, const int port)
{
    beginGroup( jid );
    setValue( key_port, QVariant(port) );
    endGroup();
}


/*-----------*/

QString MySettings::getStatus()
{
    beginGroup( group_main );
    QVariant ret = value( key_status, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setStatus( const QString &status)
{
    beginGroup( group_main );
    setValue( key_status, QVariant(status) );
    endGroup();
}


/*-----------*/

QString MySettings::getStatusText()
{
    beginGroup( group_main );
    QVariant ret = value( key_status_text, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setStatusText( const QString &status_text)
{
    beginGroup( group_main );
    setValue( key_status_text, QVariant(status_text) );
    endGroup();
}

/*-----------*/

QString MySettings::getSndIncomingMsg()
{
    beginGroup( group_main );
    QVariant ret = value( key_sndIncomingMsg, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setSndIncomingMsg( const QString &f )
{
    beginGroup( group_main );
    setValue( key_sndIncomingMsg, QVariant(f) );
    endGroup();
}

/*-----------*/

QString MySettings::getSndOutcomingMsg()
{
    beginGroup( group_main );
    QVariant ret = value( key_sndOutcomingMsg, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setSndOutcomingMsg( const QString &f )
{
    beginGroup( group_main );
    setValue( key_sndOutcomingMsg, QVariant(f) );
    endGroup();
}


/*------------------------*/

bool MySettings::notifyConnection()
{
    beginGroup( group_main );
    QVariant ret = value( key_notifyConnection, true );
    endGroup();
    return ret.toBool();
}

void MySettings::setNotifyConnection( const bool def )
{
    beginGroup( group_main );
    setValue( key_notifyConnection, QVariant(def) );
    endGroup();
}

/*------------------------*/

bool MySettings::notifyOnline()
{
    beginGroup( group_main );
    QVariant ret = value( key_notifyOnline, true );
    endGroup();
    return ret.toBool();
}

void MySettings::setNotifyOnline( const bool def )
{
    beginGroup( group_main );
    setValue( key_notifyOnline, QVariant(def) );
    endGroup();
}

/*------------------------*/

bool MySettings::notifyOffline()
{
    beginGroup( group_main );
    QVariant ret = value( key_notifyOffline, true );
    endGroup();
    return ret.toBool();
}

void MySettings::setNotifyOffline( const bool def )
{
    beginGroup( group_main );
    setValue( key_notifyOffline, QVariant(def) );
    endGroup();
}

/*------------------------*/

bool MySettings::notifyMessage()
{
    beginGroup( group_main );
    QVariant ret = value( key_notifyMessage, true );
    endGroup();
    return ret.toBool();
}

void MySettings::setNotifyMessage( const bool def )
{
    beginGroup( group_main );
    setValue( key_notifyMessage, QVariant(def) );
    endGroup();
}

/*------------------------*/

bool MySettings::notifyTyping()
{
    beginGroup( group_main );
    QVariant ret = value( key_notifyTyping, true );
    endGroup();
    return ret.toBool();
}

void MySettings::setNotifyTyping( const bool def )
{
    beginGroup( group_main );
    setValue( key_notifyTyping, QVariant(def) );
    endGroup();
}

/*-----------*/

QString MySettings::getSndBuddyLogsIn()
{
    beginGroup( group_main );
    QVariant ret = value( key_sndBuddyLogsIn, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setSndBuddyLogsIn( const QString &f )
{
    beginGroup( group_main );
    setValue( key_sndBuddyLogsIn, QVariant(f) );
    endGroup();
}

/*-----------*/

QString MySettings::getSndBuddyLogsOut()
{
    beginGroup( group_main );
    QVariant ret = value( key_sndBuddyLogsOut, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setSndBuddyLogsOut( const QString &f )
{
    beginGroup( group_main );
    setValue( key_sndBuddyLogsOut, QVariant(f) );
    endGroup();
}

/*-----------*/

bool MySettings::reconnectWhenError()
{
    beginGroup( group_main );
    QVariant ret = value( key_reconnectOnError, false );
    endGroup();
    return ret.toBool();
}

void MySettings::setReconnectWhenError(const bool isSet)
{
    beginGroup( group_main );
    setValue( key_reconnectOnError, QVariant(isSet) );
    endGroup();
}

/*-----------*/

int MySettings::keepAliveInterval()
{
    beginGroup( group_main );
    QVariant ret = value( key_keepAliveInterval, 60 );
    endGroup();
    bool ok = false;
    int retValue = ret.toInt( &ok );
    if( ok )
        return retValue;
    else
        return 60;
}

void MySettings::setKeepAliveInterval(const int isSet)
{
    beginGroup( group_main );
    setValue( key_keepAliveInterval, QVariant(isSet) );
    endGroup();
}


/*-----------*/
QString MySettings::getSndChatSaysMyName()
{
    beginGroup( group_main );
    QVariant ret = value( key_sndChatSome1SaysMyName, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setSndChatSaysMyName( const QString &f )
{
    beginGroup( group_main );
    setValue( key_sndChatSome1SaysMyName, QVariant(f) );
    endGroup();
}


/*-----------*/
QString MySettings::getSndChatPersonEnters()
{
    beginGroup( group_main );
    QVariant ret = value( key_sndChatPersonEnters, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setSndChatPersonEnters( const QString &f )
{
    beginGroup( group_main );
    setValue( key_sndChatPersonEnters, QVariant(f) );
    endGroup();
}


/*-----------*/
QString MySettings::getSndChatPersonLeaves()
{
    beginGroup( group_main );
    QVariant ret = value( key_sndChatPersonLeaves, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setSndChatPersonLeaves( const QString &f )
{
    beginGroup( group_main );
    setValue( key_sndChatPersonLeaves, QVariant(f) );
    endGroup();
}


/*-----------*/
QString MySettings::getSndChatITalk()
{
    beginGroup( group_main );
    QVariant ret = value( key_sndChatITalk, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setSndChatITalk( const QString &f )
{
    beginGroup( group_main );
    setValue( key_sndChatITalk, QVariant(f) );
    endGroup();
}


/*-----------*/
QString MySettings::getSndChatOthersTalk()
{
    beginGroup( group_main );
    QVariant ret = value( key_sndChatOthersTalk, QString("") );
    endGroup();
    return ret.toString();
}

void MySettings::setSndChatOthersTalk( const QString &f )
{
    beginGroup( group_main );
    setValue( key_sndChatOthersTalk, QVariant(f) );
    endGroup();
}



/*------------------------*/

bool MySettings::vibrationMessageRecv()
{
    beginGroup( group_main );
    QVariant ret = value( key_vibrMessageRecv, false );
    endGroup();
    return ret.toBool();
}

void MySettings::setVibrationMessageRecv(const bool isSet)
{
    beginGroup( group_main );
    setValue( key_vibrMessageRecv, QVariant(isSet) );
    endGroup();
}

/*------------------------*/

bool MySettings::vibrationMessageSent()
{
    beginGroup( group_main );
    QVariant ret = value( key_vibrMessageSent, false );
    endGroup();
    return ret.toBool();
}

void MySettings::setVibrationMessageSent(const bool isSet)
{
    beginGroup( group_main );
    setValue( key_vibrMessageSent, QVariant(isSet) );
    endGroup();
}

/*------------------------*/

bool MySettings::vibrationChatSaysMyName()
{
    beginGroup( group_main );
    QVariant ret = value( key_vibrChatSaysMyName, false );
    endGroup();
    return ret.toBool();
}

void MySettings::setVibrationChatSaysMyName(const bool isSet)
{
    beginGroup( group_main );
    setValue( key_vibrChatSaysMyName, QVariant(isSet) );
    endGroup();
}

/*------------------------*/

bool MySettings::vibrationChatOthersTalk()
{
    beginGroup( group_main );
    QVariant ret = value( key_vibrChatOthersTalk, false );
    endGroup();
    return ret.toBool();
}

void MySettings::setVibrationChatOthersTalk(const bool isSet)
{
    beginGroup( group_main );
    setValue( key_vibrChatOthersTalk, QVariant(isSet) );
    endGroup();
}



/*-----------*/
QString MySettings::getSmilesTheme()
{
    beginGroup( group_main );
    QVariant ret = value( key_smilesTheme, QString("/opt/meegim/smiles/theme.xml") );
    endGroup();
    return ret.toString();
}

void MySettings::setSmilesTheme( const QString &f )
{
    beginGroup( group_main );
    setValue( key_smilesTheme, QVariant(f) );
    endGroup();
}
